<?php
namespace jy_area_dai;
defined('BY_JYA') or exit('error');
/*
	经销插件会员类
	如果将经销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、经销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为经销商时，修改上级的经销商数量相关数据
*/
class member extends \plugin{
	public function checklogin($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		cfc('core')->checklogin();
		$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
		if(!$member['mid']){
			$this->register(array('mid'=>$mid));
			$member=$this->get('member',array('mid'=>$mid));
		}else{
			//业务员刷新
			if(!$member['type'] && $member['is_agent'] && $member['dian_mid']){
				$this->update('member',array('type'=>1),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
				$member['type']=1;
			}
		}
		return $member;
	}
	public function checkmerch($merchid=0){
		if($merchid){
			$isup=1;
		}
		$merchid=$merchid?:$_SESSION['merchid'];
		if(!$_SESSION['uid']){
			$url=custom_url('jy_merch/manage.login/',true);
			exi('请先登录','error',$url);
		}
		if($merchid){
			$mid = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),'mid');
			if($mid !=$_SESSION['uid']){
				unset($_SESSION['merchid']);
				$merchid=0;
			}
		}
		if(!$merchid){
			$merchid = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
		}
		if($isup){
			$_SESSION['merchid']=$merchid;
		}
		return $merchid;
	}
	public function up_footernav($member=array()){
		if(!$member['mid']){
			$member=$this->get('member',array('mid'=>$_SESSION['uid']));
		}
		$footernav=$this->setting['footernav'];
		return $footernav;
		if(!$member['type'] && $member['is_agent']){
			$footernav[]=array('title'=>'经销中心','icon'=>'icon-yiban','action'=>'jy_area_dai/agent/');
		}
		if($member['type']==1 && $member['is_agent']){
			$footernav[]=array('title'=>'业务中心','icon'=>'icon-yiban','action'=>'jy_area_dai/yewu/');
		}
		if($member['type']==2){
			$footernav[]=array('title'=>'我的门店','icon'=>'icon-dianpu','action'=>'jy_merch/manage.index/');
		}
		return $footernav;
	}
	/*
		核实经销商,$isset无条件成为经销商
	*/
	public function checkmember($mid=0,$isexi=false,$isset=false){
			$this->checklogin();
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'经销商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('mid'=>$mid));
			}
			if(!$member['is_agent']){
				//如果成为经销商无条件，此处自动申请成为经销商
				if($this->setting['become']==0 || $isset){
					//经销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->setting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待！','error','jy_area_dai/apply/');
					}elseif($isset){
						return true;
					}
				}elseif($this->setting['become']==2){
					//消费次数
					$buynums=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
					if($buynums>=$this->setting['num']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_area_dai/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_area_dai/apply/');
					}
				}elseif($this->setting['become']==3){
					//消费金额
					$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
					if($buyfee>=$this->setting['money']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_area_dai/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_area_dai/apply/');
					}
				}elseif($this->setting['become']==4){
					//购买指定商品
					$become_goods=$this->setting['become_goods'];
					foreach($become_goods as &$bgv){
						$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
						if($isbuy>0){
							$ispass=true;
							$this->apply_agent($member['mid']);
							break;
						}
					}
					if($this->setting['become_check'] && $isexi && $ispass){
						exi('您的申请，正在审核请耐心等待！','error','jy_area_dai/apply/');
					}elseif($isexi && !$ispass){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_area_dai/apply/');
					}
				}else{
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_area_dai/apply/');
					}
				}
			}elseif($member['stop'] && $isexi){
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_area_dai/apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($this->setting['become_reg']==1){
					exi('请先完善信息','error','jy_area_dai/info/');
				}else{
					
				}
			}
			return $member;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_area_dai_member',$update,array('mid'=>$mid));
		return true;
	}
	public function check_myshop($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=model('core/member')->basic($mid);
		$getmember=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($getmember['is_agent']){
			$myshop=$this->get('shop',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if(!$myshop['id']){
				$data=array(
					'logo'=>$member['avatar'],
					'title'=>$member['title']?:$member['nickname'],
					'mid'=>$member['mid'],
					'uniacid'=>$_SESSION['uniacid'],
				);
				pdo_insert('jy_area_dai_shop',$data);
				return true;
			}
		}
		return false;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('经销商会员注册失败，缺少会员ID.');
		}
		$ismid=$this->get('member',array('mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		$this->insert('member',$member);
		if($member['agent_id']){
			$this->set_parent($member['mid'],$member['agent_id']);
		}
		if(!$member['agent_id'] && $_SESSION['this']=='custom' && $this->setting['become_child']==1){
			$agent_id=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'fromuser');
			if($agent_id){
				$this->set_parent($member['mid'],$agent_id);
			}
		}
		//如果成为经销商无条件，此处自动申请成为经销商
		if($this->setting['become']==0){
			//经销商无需审核，此处自动审核
			$this->apply_agent($member['mid'],true);
		}
	}
	/*
		设置临时销售关系
	*/
	public function upsale_id($sale_id=0,$member=array()){
		if(!$sale_id){
			return false;
		}
		$dianmember=$this->get('member',array('mid'=>$sale_id));
		if($dianmember['dian_mid']){
			$member_update=array(
				'sale_id'=>$sale_id
			);
			if(!$member['agent_id']){
				$member_update['agent_id']=$sale_id;
			}
			$this->update('member',$member_update,array('mid'=>$_SESSION['uid']));
		}else if($dianmember['level_id'] && $dianmember['is_agent'] && !$dianmember['type']){
				/*$level_id=$dianmember['level_id'];
				$level=$this->get('level',array('id'=>$level_id));
				$parameter=array(
					'params'=>array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level['level']),
					'orderby'=>'level desc',
					'limit'=>'1',
				);
				$levels=$this->get_list('level',$parameter);
				$lastlevel_id=$levels[0]['id'];*/
				$member_update=array(
					'agent_id'=>$sale_id,
					'daili_id'=>$sale_id,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('mid'=>$_SESSION['uid']));
		}
		return true;
	}
	/*
		设置上下级关系
	*/
	public function set_parent($mid,$parent_id,$ischange=false){
		$member=$this->get('member',array('mid'=>$mid),array('level_id','agent_id'));
		$old_agent_id=$member['agent_id'];
		$isuser=pdo_getcolumn('core_members',array('id'=>$parent_id),'id');
		//如果不存在该会员，或已经有上级了，取消执行。
		if(($old_agent_id || !$isuser) && !$ischange){
			return true;
		}
		//如果他的上级是我
		$ta_agent_id=$this->getcolumn('member',array('mid'=>$parent_id),'agent_id');
		if($ta_agent_id==$mid){
			return true;
		}
		//如果上级是自己，上级ID归零
		if($mid==$parent_id){
			return true;
		}
		$this->update('member',array('agent_id'=>$parent_id),array('mid'=>$mid));
		//业务员获得奖励
		$this->updownfee($parent_id);
		//上级获得抽奖机会
		$gamepost=array(
			'type'=>8,
			'level_id'=>$member['level_id'],
		);
		model('jy_weishop/game')->up_addgame($gamepost,$parent_id);
		if($this->setting['level']>0){
			$agent_id=$old_agent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
			$agent_id=$parent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				//只有一级才增加一级下级
				if($i==0){
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
						'member_num_next +='=>1,
						'agent_num_next +='=>1,
					);
				}else{
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
					);
				}
				$this->update('member',$member_update,array('mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				model('jy_weishop/notice')->notice_fenxiao_xiajiadd($mid,$agent_id);
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
		}
		return true;
	}
	/*
		更新等级
	*/
	public function upshop_member($member){
			$level=$this->get('level',array('id'=>$member['level_id']));
			if($level['set_group']){
				pdo_update('jy_weishop_member',array('level'=>$level['set_group']),array('mid'=>$member['mid']));
			}
			return true;
	}
	/*
		申请成为经销商
	*/
	public function apply_agent($mid,$noapply=false,$level_id=0){
		$apply=$this->get('apply',array('mid'=>$mid));
		$apply_id=$apply['id'];
		if($apply['status']==1){
				$member_update=array(
					'is_agent'=>1,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('mid'=>$apply['mid']));
				$member=$this->get('member',array('mid'=>$apply['mid']));
				$this->upshop_member($member);
				$this->model('notice')->notice_fenxiao_add($mid);
				return true;
		}elseif(!$apply_id){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'level_id'=>$level_id,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
			if($this->setting['become_check'] && $this->setting['adminmid']){
				$this->model('notice')->notice_fenxiao_apply($mid,$this->setting['adminmid']);
			}
		}
		if(!$this->setting['become_check'] && $apply['status']!=2){
			//经销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		if($apply['status']==2){
			$this->update('apply',array('status'=>0),array('id'=>$apply_id));
		}
		return true;
	}
	/*
		审核经销商
	*/
	public function check_apply($apply_id,$status,$remark='',$daili_id=0,$is_shiti=0){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member=$this->get('member',array('mid'=>$apply['mid']));
			$agent_id=$member['agent_id'];
			if($agent_id && !$daili_id){
				$dian_mid=$this->getcolumn('member',array('mid'=>$agent_id),'dian_mid');
				$daili_id=$dian_mid?:$agent_id;
			}
			if($apply['level_id']){
				$this->dai_apply($apply,$agent_id,true);
			}
			/*if($daili_id){
				$level_id=$this->getcolumn('member',array('mid'=>$daili_id),'level_id');
				$level=$this->get('level',array('id'=>$level_id));
				$parameter=array(
					'params'=>array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level['level']),
					'orderby'=>'level desc',
					'limit'=>'1',
				);
				$levels=$this->get_list('level',$parameter);
				$lastlevel_id=$levels[0]['id'];
			}*/
			//'level_id'=>$lastlevel_id,
			$member_update=array(
				'is_agent'=>1,
				'is_shiti'=>$is_shiti,
				'daili_id'=>$daili_id,
				'agenttime'=>TIMESTAMP,
			);
			$this->update('member',$member_update,array('mid'=>$apply['mid']));
			$this->upshop_member($member);
			//业务员获得奖励
			if($agent_id !=$daili_id){
				$this->upagentfee($agent_id,$apply['mid']);
			}
			//添加门店
			model('jy_merch/model')->checkadd_merch($apply['mid']);
			//审核通过，此处增加上线的相关数据
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_update=array('agent_num +='=>1);
				if($i==0){
					$agent_update['agent_num_next +=']=1;
				}
				$this->update('member',$agent_update,array('mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		禁用经销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复经销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
	/*
		获取会员的各项参数
		
		问题：开启经销内购时
			推广订单如何算？
			下线如何算？
			
			计算下线时，经销级数在设置的级数基础上-1，其他不变
			计算推广订单时，在上面的基础上+自己的订单
	*/
	public function get_member($mid){
		$res=array();
		//获取一级下线数量
		$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid),array('mid','is_agent'),'mid');		
		if(!$members){
			$res=array(
				'member_num'=>'0',
				'agent_num'=>'0',
				'order_price'=>'0',
				'order_num'=>'0',
				'myorder_price'=>'0',
				'order_price_next'=>'0',
				'order_num_next'=>'0',
				'member_num_next'=>'0',
				'agent_num_next'=>'0',
			);
			return $res;
		}
		//获取直属下级经销商数量
		$res['agent_num_next']=$this->count('member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$mid));
		$res['member_num_next']=count($members);
		$member_ids=array_keys($members);
		$res['order_num_next']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price_next']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		//获取下线总数，向下遍历系统设置的指定经销层数
		$commission_level=intval($this->setting['level']);
		if($this->setting['self_commission']){
			//开启经销内购
			$commission_level=$commission_level-1;
		}
		if($commission_level>1){
			$next_ids=$member_ids;
			for($i=1;$i<$commission_level;$i++){
				$next_members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$next_ids),array('mid','is_agent'),'mid');
				if(!$next_members){
					break;
				}
				$members=array_merge($next_members,$members);
				$next_ids=array_keys($next_members);
				$member_ids=array_merge($next_ids,$member_ids);
			}
		}
		$res['member_num']=count($member_ids);
		$res['order_num']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		$res['myorder_price']=$this->sum('order',array('mid'=>$mid),'myorder_price');
		$res['agent_num']=0;
		foreach($members as $member){
			if($member['is_agent']){
				$res['agent_num']++;
			}
		}
		if($this->setting['self_commission']){
			//开启经销内购，此处加上用户自己的订单作为推广订单的一部分
			$self_order_num=$this->count('order',array('mid'=>$mid));
			$self_order_money=$this->sum('order',array('mid'=>$mid),'order_price');
			$res['order_num_next']=$self_order_num;
			$res['order_price_next']=$self_order_money;
			$res['order_num']+=$self_order_num;
			$res['order_price']+=$self_order_money;
		}
		return $res;
	}
	/*
		升级经销商等级
		先检测会员身份是否为正常经销商
		然后获取经销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级经销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$orderid=0){
		//
		$member=$this->get('member',array('mid'=>$mid));
		if(!$member['is_agent'] || $member['stop']){
			return;
		}
		$level=$this->get('level',array('id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);
		$levels=$this->get_list('level',$parameter);
		$next_level=$levels[0];
		if(!$next_level){
			return;
		}
		$conditions=json_decode($next_level['conditions'],true);
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的经销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		foreach($level_up_types as $type){
			if($member[$type]<$conditions[$type]){
				$up=false;
				break;
			}
			if($type=='buy_goods' && $next_level['goods_ids']){
				$goods_ids=json_decode($next_level['goods_ids'],true);
				$goods_num=$next_level['goods_num']?:1;
				foreach($goods_ids as $gv){
						$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$mid,'goodsid'=>$gv['id']),'total');
						if($isbuynum>=$goods_num){
							$ispass=true;
							break;
						}
				}
				if(!$ispass){
					$up=false;
				}
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			if($next_level['creditfee']>0){
				model('core/member')->member_credit($mid,'credit2',$next_level['creditfee'],true,'jy_area_dai','达到指定经销等级获得奖励');
			}
			$this->update('member',$update,array('mid'=>$mid));
			$member['level_id']=$next_level['id'];
			$this->upshop_member($member);
		}
		return true;
	}
	public function min_agent_level($mid=0){
		//
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('mid'=>$mid));
		//开启有效期后，降级
		if($this->setting['is_leveltime'] && $this->setting['leveltime']){
			if($member['t_time'] && $member['t_time']<TIMESTAMP){
				$level=$this->get('level',array('id'=>$member['level_id']));
				$this->update('member',array('level_id'=>$level['t_level_id'],'t_time'=>0),array('mid'=>$mid));
			}
		}
	}
	/*
		核实指定等级活动
	*/
	public function upgametime($mid_ids=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(count($mid_ids)){
			foreach($mid_ids as $mv){
				$level_id=$this->getcolumn('member',array('mid'=>$mv),'level_id');
				$gamepost=array(
					'type'=>8,
					'level_id'=>$level_id
				);
				//model('jy_weishop/game')->up_addgame($gamepost,$mid);
			}
		}
	}
	/*
		邀请奖励
	*/
	public function upyao_fee($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($this->setting['yao_open']){
			foreach($this->setting['yao_set'] as $k=>$yv){
				$num=$this->pdo_count('member',array('level_id'=>$yv['level_id'],'agent_id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
				if($yv['fee']>0 && $yv['num'] && $yv['num']<=$num){
					$isget=$this->pdo_count('commission',array('ordersn'=>'yao_set'.$k,'level'=>17,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
					if($isget>0){
						continue;
					}
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'level'=>17,
						'ordersn'=>'yao_set'.$k,
						'money'=>$yv['fee'],
						'createtime'=>TIMESTAMP,
						'status'=>0,
					);
					$this->insert('commission',$new_commission);
				}
			}
		}
		return false;
	}
	/*
		发展经销奖励
	*/
	public function upagentfee($mid=0,$from_id=0){
		if(!$from_id){
			return false;
		}
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('mid'=>$mid));
		$dianinfo=$this->get('member',array('mid'=>$member['dian_mid']));
		$isget=$this->get('commission',array('mid'=>$mid,'is_key'=>$from_id,'type'=>24));
		if(!$isget['id']){
			$downdata=json_decode($dianinfo['downdata'],true);
			if($downdata['agentonefee']>0){
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mid,
							'level'=>1,
							'money'=>$downdata['agentonefee'],
							'createtime'=>TIMESTAMP,
							'is_key'=>$from_id,
							'type'=>24,
							'status'=>1,
							'send_feetype'=>1,
							'dian_mid'=>$member['dian_mid'],
						);
						$this->insert('commission',$new_commission);
			}
		}
		//平台发
		$isget=$this->get('commission',array('mid'=>$mid,'is_key'=>$from_id,'type'=>26));
		if(!$isget['id']){
			if($this->setting['agentonefee']>0){
				$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mid,
							'level'=>1,
							'money'=>$this->setting['agentonefee'],
							'createtime'=>TIMESTAMP,
							'is_key'=>$from_id,
							'type'=>24,
							'status'=>1,
							'send_feetype'=>0,
							'dian_mid'=>$member['dian_mid'],
				);
				$this->insert('commission',$new_commission);
			}
		}
		return true;
	}
	/*
		发展客户奖励
	*/
	public function updownfee($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('mid'=>$mid));
		$downnum=$this->count('member',array('agent_id'=>$mid),'id');
		$dianinfo=$this->get('member',array('mid'=>$member['dian_mid']));
		$downdata=json_decode($dianinfo['downdata'],true);
		if(count($downdata['down'])){
			paixu($downdata['down'],'num','desc');
			foreach($downdata['down'] as $dv){
				if($downnum>=$dv['num']){
					$isget=$this->get('commission',array('mid'=>$mid,'is_key'=>$dv['num'],'type'=>23));
					if($isget['id']){
						continue;
					}
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'level'=>1,
						'money'=>$dv['fee'],
						'createtime'=>TIMESTAMP,
						'is_key'=>$dv['num'],
						'type'=>23,
						'status'=>1,
						'send_feetype'=>1,
						'dian_mid'=>$member['dian_mid'],
					);
					$this->insert('commission',$new_commission);
					break;
				}
			}
		}
		//平台发放
		if(count($this->setting['commissions'])){
			paixu($this->setting['commissions'],'num','desc');
			foreach($this->setting['commissions'] as $dv){
				if($downnum>=$dv['num']){
					$isget=$this->get('commission',array('mid'=>$mid,'is_key'=>$dv['num'],'type'=>25));
					if($isget['id']){
						continue;
					}
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'level'=>1,
						'money'=>$dv['fee'],
						'createtime'=>TIMESTAMP,
						'is_key'=>$dv['num'],
						'type'=>25,
						'status'=>1,
						'send_feetype'=>0,
						'dian_mid'=>$member['dian_mid'],
					);
					$this->insert('commission',$new_commission);
					break;
				}
			}
		}
		return true;
	}
	public function getdetail($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$user=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','nickname','avatar','credit1','credit1','credit2','credit3','credit4','credit5','credit6'));
		return $user;
	}
	public function dai_apply($apply=array(),$agent_id=0,$isjian=false){
		if(!$agent_id){
			return false;
		}
		$level=$this->get('level',array('id'=>$apply['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$goods_ids=json_decode($level['goods_ids'],true);
		foreach($goods_ids as $goods){
			$goods=pdo_get('jy_area_dai_mygoods',array('mid'=>$agent_id,'goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
			if(!$goods['id']){
				continue;
			}else{
				$goods['goods_num']=$goods['goods_num']?:1;
				if($goods['total']<$goods['goods_num']){
					exi('上级库存不足，不可开通！','error');
				}
				$ispass=true;
				if($isjian){
					pdo_update('jy_area_dai_mygoods',array('total -='=>$goods['goods_num']),array('mid'=>$agent_id,'goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		if(!$ispass){
			exi('上级没有该等级升级条件的库存，不可开通！','error');
		}
		return true;
	}
}